import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from base.base import Base
from handle.handle_yaml import login_yaml_handle
from handle.handle_csv import handerUserData


class Login(Base):

    def __init__(self, driver, url):
        """
        初始化
        :param driver:
        :param url:
        """
        super().__init__(driver)
        self.url = url

    def login(self):
        """
        登录模块
        :return:
        """
        ele = login_yaml_handle()

        # 打开url
        self.open_url(self.url)

        # 清空，输入用户名
        self.find_element(By.XPATH, ele["login"]["account"]).clear()
        self.find_element(By.XPATH, ele["login"]["account"])\
            .send_keys(handerUserData()["username"])

        # 清空，输入密码
        self.find_element(By.XPATH, ele["login"]["password"]).clear()
        self.find_element(By.XPATH, ele["login"]["password"])\
            .send_keys(handerUserData()["password"])

        # 输入验证码 ---略

        # 点击登录按钮
        self.find_element(By.XPATH, ele["login"]["button"]).click()

        # 验证登录
        self.find_element(By.XPATH, ele["login"]["checklogin_select"]).click()
        txt = self.find_element(By.XPATH, ele["login"]["checklogin_user"]).text
        return txt
        # 获取cookies 或者 token 保存到redis


if __name__ == "__main__":
    driver = webdriver.Chrome()
    url = "http://ecp.gzctdi-test.com/tenement/"
    lg = Login(driver, url)
    txt = lg.login()
    time.sleep(4)
    driver.quit()
    print(txt)








